#include "Config_Test.h"

#include "server/misc/Config.h"
#include <Poco/Exception.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
/*********************************************************/
std::string ConfigTest::name()
{
	return("Config");
}
/*********************************************************/
bool ConfigTest::runUnitTests()
{
	bool result = true;

	if ( !singleInstanceTest() ) result = false;
	if ( !defaultValuesTest() ) result = false;

	return( result ) ;
}
/*********************************************************/
bool ConfigTest::defaultValuesTest()
{
	try {
		std::cout << "Running: defaultValuesTest()";
		
		// Delete the config file
		remove("config.ini");
			
		Config& cfg = Config::Instance();
		cfg.reparse();

		std::ifstream ifile("config.ini");
		if ( ifile.is_open() ) {
			ifile.close();
			std::cout << "... success!" << std::endl;
			return( true );
		} else {
			std::cout << "... failed! No default file was written to the disk!" << std::endl;
			return( false );
		}
	} catch(Poco::Exception& e) {
		std::cout << " caught exception: " << e.displayText() << std::endl;
		return( false );
	} catch(std::exception& e) {
		std::cout << " caught exception: " << e.what() << std::endl;
		return( false );
	}
}
/*********************************************************/
bool ConfigTest::singleInstanceTest()
{
	std::cout << "Running: singleInstanceTest()";
	try {
		Config& e = Config::Instance();
		Config& ee = Config::Instance();

		if ( &e == &ee ) {
			std::cout << "... success!" << std::endl;
			return( true );
		} else {
			std::cout << "... Failed!" << std::endl;
			return( false );
		}
	} catch(Poco::Exception& e) {
		std::cout << " caught exception: " << e.displayText() << std::endl;
		return( false );
	} catch(std::exception& e) {
		std::cout << " caught exception: " << e.what() << std::endl;
		return( false );
	}
}
